package com.visikard.ui.fragment;

import java.lang.reflect.Field;
import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;

public class MyDatePickerDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener {

	SelectDateCallback mCallback;
	public static final boolean ANDROID_HONEYCOMB_ORHIGHER = android.os.Build.VERSION.SDK_INT >= 11;
	private int mYear;
	private int mMonth;
	private int mDay;
	int StartOrEnd;

	public MyDatePickerDialog(SelectDateCallback callback, int action) {
		this.mCallback = callback;
		this.StartOrEnd = action;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);

		DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, mYear, mMonth, mDay);
		// if (StartOrEnd == 2) {
		// dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
		// }
		if (ANDROID_HONEYCOMB_ORHIGHER) {
			// For Android 11 or higher
			if (StartOrEnd == 2) {
				dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
			}
		}
		else {
			if (StartOrEnd == 2) {
				final Calendar minCal = Calendar.getInstance();
				final int minYear = minCal.get(Calendar.YEAR);
				final int minMonth = minCal.get(Calendar.MONTH);
				final int minDay = minCal.get(Calendar.DAY_OF_MONTH);
				minCal.set(minYear, minMonth, minDay);

				Field mDatePickerField;
				try {
					mDatePickerField = dialog.getClass().getDeclaredField("mDatePicker");
					mDatePickerField.setAccessible(true);
					DatePicker mDatePickerInstance = (DatePicker) mDatePickerField.get(dialog);
					// Try to hide the day field
					Field datePickerFields[] = mDatePickerField.getType().getDeclaredFields();
					for (Field datePickerField : datePickerFields) {
						if ("mDayPicker".equals(datePickerField.getName())
								|| "mDaySpinner".equals(datePickerField.getName())) {
							datePickerField.setAccessible(true);
							Object dayPicker = new Object();
							dayPicker = datePickerField.get(mDatePickerInstance);
							((View) dayPicker).setVisibility(View.GONE);
						}
					}
					mDatePickerInstance.init(mYear, mMonth, mDay, new OnDateChangedListener() {

						public void onDateChanged(DatePicker view, int year, int month, int day) {
							Calendar newDate = Calendar.getInstance();
							newDate.set(year, month, day);

							if (minCal.after(newDate)) {
								view.init(minYear, minMonth, minDay, this);
							}
						}
					});
				}
				catch (NoSuchFieldException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
		return dialog;
	}

	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
		// TODO Auto-generated method stub
		if (view.isShown()) {
			mCallback.onSelectDateCallback(dayOfMonth, (monthOfYear + 1), year, StartOrEnd);
		}

	}

	public interface SelectDateCallback {
		public void onSelectDateCallback(int day, int month, int year, int StartOrEnd);
	}
}
